import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../layout/index.vue'
import Cookies from "js-cookie";
const whiteList = ['/login']

/* const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error=> error)
} */

Vue.use(VueRouter)

export const routes = [{
        path: '/',
        redirect: '/home'
    },
    {
        path: "/login",
        name: "login",
        component: () =>
            import ("../views/login.vue")
    },
    {
        path: '/404',
        component: (resolve) => require(['@/views/error/404'], resolve),
        hidden: true
    },
    {
        path: '/401',
        component: (resolve) => require(['@/views/error/401'], resolve),
        hidden: true
    },
    {
        path: '/',
        name: 'layout',
        component: Layout,
        children: [
            /**首页 */
            {
                path: "/home",
                name: "home",
                meta: {
                    title: '首页'
                },
                component: () =>
                    import ('../views/home.vue')
            },
            /**基础信息 */
            {
                path: '/dict',
                name: 'dict',
                meta: {
                    title: '数据字典',
                },
                component: () =>
                    import ('@/views/system/dict')
            },
            {
                path: '/companyInfo',
                name: 'companyInfo',
                meta: {
                    title: '企业信息',
                },
                component: () =>
                    import ('@/views/system/companyInfo')
            },
            {
                path: '/adminSet',
                name: 'adminSet',
                meta: {
                    title: '用户管理',
                },
                component: () =>
                    import ('@/views/system/adminSet')
            },
            {
                path: '/adminRole',
                name: 'adminRole',
                meta: {
                    title: '角色管理',
                },
                component: () =>
                    import ('@/views/system/adminRole')
            },
            {
                path: '/organize',
                name: 'organize',
                meta: {
                    title: '部门管理',
                },
                component: () =>
                    import ('@/views/system/organize')
            },
            {
                path: '/personAdmin',
                name: 'personAdmin',
                meta: {
                    title: '员工管理',
                },
                component: () =>
                    import ('@/views/system/personAdmin')
            },
            {
                path: '/outPersonnel',
                name: 'outPersonnel',
                meta: {
                    title: '外委人员管理',
                },
                component: () =>
                    import ('@/views/system/outPersonnel')
            },
            {
                path: '/industryClass',
                name: 'industryClass',
                meta: {
                    title: '行业资源分类',
                },
                component: () =>
                    import ('@/views/system/industryClass')
            },
            {
                path: '/classification',
                name: 'classification',
                meta: {
                    title: '课程分类',
                },
                component: () =>
                    import ('@/views/system/classification')
            },
            {
                path: '/outUnitAdmin',
                name: 'outUnitAdmin',
                meta: {
                    title: '外委单位管理',
                },
                component: () =>
                    import ('@/views/system/outUnitAdmin')
            },
            {
                path: '/banner',
                name: 'banner',
                meta: {
                    title: '轮播图管理',
                },
                component: () =>
                    import ('@/views/system/banner')
            },
            /**培训管理 */
            {
                path: '/teacherList',
                name: 'teacherList',
                meta: {
                    title: '讲师管理',
                },
                component: () =>
                    import ('@/views/trainAdmin/teacherList')
            },
            {
                path: '/teamSet',
                name: 'teamSet',
                meta: {
                    title: '企业员工班组',
                },
                component: () =>
                    import ('@/views/trainAdmin/teamSet')
            },
            {
                path: '/teamOutset',
                name: 'teamOutset',
                meta: {
                    title: '外委人员班组',
                },
                component: () =>
                    import ('@/views/trainAdmin/teamOutset')
            },
            {
                path: '/trainCourse',
                name: 'trainCourse',
                meta: {
                    title: '课程管理',
                },
                component: () =>
                    import ('@/views/trainAdmin/trainCourse')
            },
            {
                path: '/trainPlan',
                name: 'trainPlan',
                meta: {
                    title: '培训计划',
                },
                component: () =>
                    import ('@/views/trainAdmin/trainPlan')
            },
            {
                path: '/trainAct',
                name: 'trainAct',
                meta: {
                    title: '培训任务',
                },
                component: () =>
                    import ('@/views/trainAdmin/trainAct')
            },
            {
                path: '/personTrainNote',
                name: 'personTrainNote',
                meta: {
                    title: '培训记录',
                },
                component: () =>
                    import ('@/views/trainAdmin/personTrainNote')
            },
            {
                path: '/trainMatrix',
                name: 'trainMatrix',
                meta: {
                    title: '培训矩阵',
                },
                component: () =>
                    import ('@/views/trainAdmin/trainMatrix')
            },
            /**考试管理 */
            {
                path: '/problemBaseAdmin',
                name: 'problemBaseAdmin',
                meta: {
                    title: '试题管理',
                },
                component: () =>
                    import ('@/views/check/problemBaseAdmin')
            },
            {
                path: '/problemSet',
                name: 'problemSet',
                meta: {
                    title: '试卷管理',
                },
                component: () =>
                    import ('@/views/check/problemSet')
            },
            {
                path: '/testTask',
                name: 'testTask',
                meta: {
                    title: '考试任务',
                },
                component: () =>
                    import ('@/views/check/testTask')
            },
            {
                path: '/testRecord',
                name: 'testRecord',
                meta: {
                    title: '考试记录',
                },
                component: () =>
                    import ('@/views/check/testRecord')
            },
            /**档案管理 */
            {
                path: '/personTrainRecord',
                name: 'personTrainRecord',
                meta: {
                    title: '个人培训档案',
                },
                component: () =>
                    import ('@/views/trainRecord/personTrainRecord')
            },
            {
                path: '/companyTrainRecord',
                name: 'companyTrainRecord',
                meta: {
                    title: '企业培训档案',
                },
                component: () =>
                    import ('@/views/trainRecord/companyTrainRecord')
            },
            /**证书管理 */
            {
                path: '/personnelCert',
                name: 'personnelCert',
                meta: {
                    title: '员工证书',
                },
                component: () =>
                    import ('@/views/cert/personnelCert')
            },
            {
                path: '/outPersonnelCert',
                name: 'outPersonnelCert',
                meta: {
                    title: '外委人员证书',
                },
                component: () =>
                    import ('@/views/cert/outPersonnelCert')
            },
            {
                path: '/retrainRecords',
                name: 'retrainRecords',
                meta: {
                    // title: '培训复训记录',
                    title: '复训记录管理',
                },
                component: () =>
                    import ('@/views/cert/retrainRecords')
            },
            /**违规处罚 */
            {
                path: '/punish',
                name: 'punish',
                meta: {
                    title: '违规处罚',
                },
                component: () =>
                    import ('@/views/punish/punish')
            },
            /**统计报表 */
            {
                path: '/trainReport',
                name: 'trainReport',
                meta: {
                    title: '统计报表',
                },
                component: () =>
                    import ('@/views/reports/trainReport')
            },
            /**消息管理 */
            {
                path: '/systemNotice',
                name: 'systemNotice',
                meta: {
                    title: '系统消息',
                },
                component: () =>
                    import ('@/views/notice/systemNotice')
            },
            {
                path: '/noticeMsg',
                name: 'noticeMsg',
                meta: {
                    title: '通知消息',
                },
                component: () =>
                    import ('@/views/notice/noticeMsg')
            },
            /**资料库管理 */
            {
                path: '/commonBase',
                name: 'commonBase',
                meta: {
                    title: '公共资源',
                },
                component: () =>
                    import ('@/views/database/commonBase')
            },
            {
                path: '/tradeBase',
                name: 'tradeBase',
                meta: {
                    title: '行业资源',
                },
                component: () =>
                    import ('@/views/database/tradeBase')
            },
            {
                path: '/companyBase',
                name: 'companyBase',
                meta: {
                    title: '企业资源',
                },
                component: () =>
                    import ('@/views/database/companyBase')
            },
            /**资讯公告 */
            {
                path: '/information',
                name: 'information',
                meta: {
                    title: '资讯新闻',
                },
                component: () =>
                    import ('@/views/information/information')
            },
            {
                path: '/announcement',
                name: 'announcement',
                meta: {
                    title: '通知公告',
                },
                component: () =>
                    import ('@/views/announcement/announcement')
            },
        ]
    },
    // { path: '*', redirect: '/404', hidden: true },
]

const router = new VueRouter({
    mode: 'history',
    // base: process.env.BASE_URL,
    base: '',
    routes
})

/* router.beforeEach((to, from, next) => {
    // document.title = `${to.meta.title}`;
    const role = Cookies.get('isLogin');
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        role === 'admin' ? next() : next('/403');
    } else {
        next();
    }
}); */

router.beforeEach((to, from, next) => {
    // document.title = `${to.meta.title}`;
    const isLogin = Cookies.get('isLogin');
    if (isLogin) {   //已登录
        if (to.path === '/login') {
            next({ path: '/' })
        }else {
            next()
        }
    }else {   //未登录
        if (whiteList.indexOf(to.path) !== -1) {
            // 在免登录白名单，直接进入
            next()
        }else {
            // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
            next('/login');
        }
    }
});

export default router